import store from '@/store'
import errorCode from '../apis/zniaoerrorcode.js'
import {loginPath, whiteList,tenantId} from '../config.js'
function checkState(res,resolve,reject){
	let code=res.data.code|| 200;
	if(res.data&&res.data.data&&res.data.data.status==403){
		//处理过期
		uni.showModal({
			title: "用户提示",
			content: res.data.data.content&&res.data.data.content.msg,
			success:(res)=> {
				if(res.confirm){
					store.dispatch('user/LogOut');
					uni.navigateTo({
						url:loginPath
					})
				}
			}
		})
	}else if(res.data&&res.data.data&&res.data.data.status&&res.data.data.content
		&&res.data.data.content.msg&&res.data.data.status!=200){
		uni.showModal({
			title: "温馨提示",
			content: res.data.data.content&&errorCode[res.data.data.content.code]||res.data.data.content.msg
		})
		reject(res.data)
	}else if (res.statusCode === 200&&code===200) {
		resolve(res.data)
	} else if(code===401){
		//处理过期
		uni.showModal({
			title: "用户提示",
			content: "登陆过期重新登陆？",
			success:(res)=> {
				if(res.confirm){
					store.dispatch('user/LogOut');
					uni.navigateTo({
						url:loginPath
					})
				}
			}
		})
	}else{
		if(res.data.msg){
			uni.showModal({
				title: "温馨提示",
				content: res.data.msg
			})
		}
		reject(res.data)
	}
}
export function get(url, params={}, title) {
	params.tenantId=tenantId
	return new Promise((resolve, reject) => {
		uni.showNavigationBarLoading();
		url = url + '?';
		for (const propName of Object.keys(params)) {
		  const value = params[propName];
		  var part = encodeURIComponent(propName) + "=";
		  if (value !== null && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
			  for (const key of Object.keys(value)) {
				let params = propName + '[' + key + ']';
				var subPart = encodeURIComponent(params) + "=";
				url += subPart + encodeURIComponent(value[key]) + "&";
			  }
			} else {
			  url += part + encodeURIComponent(value) + "&";
			}
		  }
		}
		url = url.slice(0, -1);
		
		uni.request({
			url,
			method: "GET",
			success: (res) => {
				checkState(res,resolve, reject)
			},
			fail: (err) => {
				uni.showModal({
					title: "错误",
					content: "服务器错误维护 或 检查网络！"
				})
				reject(err)
			},
			complete: () => {
				uni.hideNavigationBarLoading();
			}
		})
	})
}
export function shamPost(url, params, title) {
	return new Promise((resolve, reject) => {
		uni.showNavigationBarLoading();
		params.tenantId=tenantId
		url = url + '?';
		for (const propName of Object.keys(params)) {
		  const value = params[propName];
		  var part = encodeURIComponent(propName) + "=";
		  if (value !== null && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
			  for (const key of Object.keys(value)) {
				let params = propName + '[' + key + ']';
				var subPart = encodeURIComponent(params) + "=";
				url += subPart + encodeURIComponent(value[key]) + "&";
			  }
			} else {
			  url += part + encodeURIComponent(value) + "&";
			}
		  }
		}
		url = url.slice(0, -1);
		
		uni.request({
			url,
			method: "POST",
			success: (res) => {
				checkState(res,resolve, reject)
			},
			fail: (err) => {
				uni.showModal({
					title: "错误",
					content: "服务器错误维护 或 检查网络！"
				})
				reject(err)
			},
			complete: () => {
				uni.hideNavigationBarLoading();
			}
		})
	})
}

export function post(url, params, title) {
	params.tenantId=tenantId
	return new Promise((resolve, reject) => {
		uni.showNavigationBarLoading();
		uni.request({
			url,
			data: params,
			method: "POST",
			success: (res) => {
				checkState(res,resolve, reject)
			},
			fail: (err) => {
				uni.showModal({
					title: "错误",
					content: "服务器错误维护 或 检查网络！"
				})
				reject(err)
			},
			complete: () => {
				uni.hideNavigationBarLoading();
			}
		})
	})
}

export default get